package com.example.library.service;

import com.example.library.model.Book;
import com.example.library.repository.BookRepository;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "books")
public class BookService {
    private final BookRepository bookRepository;

    public BookService(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @Cacheable("booksWithRecords")
    public List<Book> getAllBooksWithBorrowRecords() {
        return bookRepository.findAll();
    }

    @Cacheable(value = "bookSearch", key = "#keyword")
    public List<Book> searchBooks(String keyword) {
        return bookRepository.findByKeyword(keyword.toLowerCase());
    }
}